﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Fynd.Hermes.Web.Objects;
using FyndSharp.Utilities.Common;
using FyndSharp.Data;
using FyndSharp.Data.Orm;
using System.Data.Common;
using System.Data;

namespace Fynd.Hermes.Web.Pages
{
    public partial class OperationLogViewer : RoleBasedPage
    {
        protected override IEnumerable<UserRole> AccreditedRoles
        {
            get
            {
                return new UserRole[] { UserRole.Administrator };
            }
        }
        protected DateTime DateFilterValue = DateTime.Now.Date;
        protected string PeopleNameFilterValue = String.Empty;
        protected List<OperationLog> ResultList = new List<OperationLog>();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString.AllKeys.Contains<string>("date"))
            {
                DateTime urlParamDate = TypeConvert.ToDateTime(Request.QueryString["date"]);
                if (urlParamDate.Equals(new DateTime()) == false)
                {
                    DateFilterValue = urlParamDate.Date;
                }
            }
            PeopleNameFilterValue = TypeConvert.ToString(Request.QueryString["name"]);
            LoadResultBy(DateFilterValue, PeopleNameFilterValue);
        }

        private void LoadResultBy(DateTime theDate, string theName)
        {
            AdoSession db = AdoSessionManager.Current["hermes"];
            bool isDbOpen = false;
            try
            {
                isDbOpen = db.Open();

                string sql = OrmObjectFactory.CreateSelectSql(typeof(OperationLog));
                sql += " WHERE operation_date >= @min_date AND operation_date < @max_date";
                if (!String.IsNullOrEmpty(theName))
                {
                    sql += " AND operator_true_name = @operator_true_name";
                }
                sql += " ORDER BY operation_date DESC";
                using (DbCommand cmd = db.CreateCommand(sql))
                {
                    cmd.Parameters.Add(db.CreateParameter("@min_date", DbType.DateTime, theDate));
                    cmd.Parameters.Add(db.CreateParameter("@max_date", DbType.DateTime, theDate.Date.AddDays(1)));
                    if (!String.IsNullOrEmpty(theName))
                    {
                        cmd.Parameters.Add(db.CreateParameter("@operator_true_name", DbType.String, theName));
                    }
                    DataTable dt = db.ExecuteDataTable(cmd);
                    foreach (DataRow row in dt.Rows)
                    {
                        ResultList.Add(OrmObjectFactory.CreateOrmObject<OperationLog>(row));
                    }
                }
            }
            finally
            {
                if (isDbOpen)
                {
                    db.Close();
                }
            }
        }
    }
}